package com.sirap.spring.boot.demo.controller.album;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.sirap.basic.tool.D;
import com.sirap.basic.util.Amaps;
import com.sirap.basic.util.IOUtil;
import com.sirap.basic.util.StrUtil;
import com.sirap.basic.util.XCodeUtil;
import com.sirap.fish.fetchers.MayImageFetchers;

@Controller
@RequestMapping(value = "/albummay")
public class AlbumMayController extends TopAlbumController {
	
	public static final String MAY_WORDS_PATH = "/may/all.words.txt";

	@Override
	@RequestMapping(value = "/list")
	public String listAlbums(HttpServletRequest request, HashMap<String, Object> map) {
		D.at();
		
		return StrUtil.occupy("album/album{0}List", type());
	}

	@RequestMapping(value = "/words")
	public String listWords(HttpServletRequest request, HashMap<String, Object> map) {
		D.at();
		
		String location = StrUtil.useSeparator(storage(), MAY_WORDS_PATH);
		
		D.pl(location);
		
		List<String> lines = IOUtil.readLinesWithUTF8(location);
		
		map.put("tags", MayImageFetchers.tagsFromList(lines));
		
		return StrUtil.occupy("album/albumMayWords");
	}

	@Override
	@ResponseBody
    @RequestMapping(value = "/page/{base64url}")
	public Map<String, Object> getAlbumsByPageUrl(@PathVariable String base64url, HttpServletRequest request) {
		D.at();
    	String niceurl = XCodeUtil.fromBase64(base64url);
    	if(StrUtil.equals("base", niceurl)) {
    		niceurl = "http://www.699mm.com/meitu/tuinvlang/";
    	}
    	D.pla(base64url, niceurl);
		Map<String, Object> map = MayImageFetchers.getAlbumsByPageUrl(niceurl);
		
		D.pjsp(map);
	
		return map;
	}

	@Override
	@RequestMapping(value = "/show/{base64url}")
	public String showAlbum(HttpServletRequest request, @PathVariable String base64url, HashMap<String, Object> map) {
		D.at();
		map.putAll(getAlbumDetail(base64url, MayImageFetchers.class));
	
		return StrUtil.occupy("album/album{0}Detail", type());
	}

	@Override
	@ResponseBody
	@RequestMapping(value = "/count/{base64url}")
	public Map<String, Object> countOfImages(HttpServletRequest request, @PathVariable String base64url) {
		D.at();
		
    	String niceurl = XCodeUtil.fromBase64(base64url);
    	D.pla(base64url, niceurl);
    	
		return Amaps.createMap("count", MayImageFetchers.countOfImages(niceurl));
	}
}
